package com.getpebble.android.common.core.util;

import android.app.ActivityManager;
import android.app.ActivityManager.RunningAppProcessInfo;
import android.content.Context;
import android.os.Binder;
import android.os.Process;
import com.getpebble.android.common.core.trace.Trace;
import com.sun.mail.smtp.SMTPMessage;
import java.util.List;

public class ProcessUtil {
    private static final String TAG = ProcessUtil.class.getSimpleName();
    private static PebbleProcess sCurrentProcess;

    static /* synthetic */ class AnonymousClass1 {
        static final /* synthetic */ int[] $SwitchMap$com$getpebble$android$common$core$util$ProcessUtil$PebbleProcess = new int[PebbleProcess.values().length];

        static {
            try {
                $SwitchMap$com$getpebble$android$common$core$util$ProcessUtil$PebbleProcess[PebbleProcess.UI.ordinal()] = 1;
            } catch (NoSuchFieldError e) {
            }
            try {
                $SwitchMap$com$getpebble$android$common$core$util$ProcessUtil$PebbleProcess[PebbleProcess.FRAMEWORK.ordinal()] = 2;
            } catch (NoSuchFieldError e2) {
            }
        }
    }

    public enum PebbleProcess {
        UI(":ui"),
        FRAMEWORK(":framework"),
        UNKNOWN("unknown");
        
        final String processName;

        private PebbleProcess(String mProcessName) {
            this.processName = mProcessName;
        }

        public static PebbleProcess from(String processName) {
            for (PebbleProcess process : values()) {
                if (processName.endsWith(process.processName)) {
                    return process;
                }
            }
            return UNKNOWN;
        }
    }

    public static synchronized PebbleProcess getProcess(Context context) {
        PebbleProcess pebbleProcess;
        synchronized (ProcessUtil.class) {
            if (sCurrentProcess != null) {
                pebbleProcess = sCurrentProcess;
            } else {
                int id = Process.myPid();
                List<RunningAppProcessInfo> procInfos = ((ActivityManager) context.getSystemService("activity")).getRunningAppProcesses();
                String processName = null;
                if (procInfos == null) {
                    throw new IllegalStateException("Running App Process Info Was Null");
                }
                for (RunningAppProcessInfo procInfo : procInfos) {
                    if (id == procInfo.pid) {
                        processName = procInfo.processName;
                        break;
                    }
                }
                Trace.debug(TAG, "processName = '" + processName + "'");
                if (processName == null) {
                    throw new IllegalStateException("Could not get process name");
                }
                pebbleProcess = PebbleProcess.from(processName);
                switch (AnonymousClass1.$SwitchMap$com$getpebble$android$common$core$util$ProcessUtil$PebbleProcess[pebbleProcess.ordinal()]) {
                    case SMTPMessage.RETURN_FULL /*1*/:
                    case SMTPMessage.RETURN_HDRS /*2*/:
                        sCurrentProcess = pebbleProcess;
                        break;
                    default:
                        throw new IllegalStateException("Unknown process: '" + processName + "'");
                }
            }
        }
        return pebbleProcess;
    }

    public static void runWithClearedIdentity(Runnable runnable) {
        long token = Binder.clearCallingIdentity();
        try {
            runnable.run();
        } finally {
            Binder.restoreCallingIdentity(token);
        }
    }
}
